<template>
  <div class="beijing">
    <div class="entirety">
      <div class="pic">
        <!-- <img src="../assets/image/login.png" alt="" /> -->
        <img src="../assets/image/icon-login.png" alt="" style="width: 410px; height: 350px;" />
      </div>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <h3 style="text-align:center;padding-bottom:20px;">修改密码</h3>
        <el-form-item prop="number">
          <el-input v-model.number="ruleForm.number" placeholder="请输入手机号" class="int">
            <i slot="prefix" class="el-icon-phone-outline" style="font-size:18px;
            padding-right: 5px;
            color:rgb(169, 170, 170);
            
            "></i></el-input>
        </el-form-item>
        <el-form-item prop="yanzhengma">

          <el-input v-model.number="ruleForm.yanzhengma" class="int" placeholder="请输入验证码" autocomplete="new-password">
            <i slot="prefix" class="el-icon-thumb" style="font-size:18px;
            padding-right: 5px;
            color:rgb(169, 170, 170);
            "></i>
            <template #suffix>
              <el-button round @click="sendCode" :disabled="ifDisable" type="text" class="button">
                <span v-if="!ifDisable">获取验证码</span>
                <span v-else> {{ selfCount }}秒后重新发送</span>
              </el-button>

            </template>
          </el-input>

        </el-form-item>
        <el-form-item prop="pass">

          <el-input type="password" v-model="ruleForm.pass" autocomplete="off" placeholder="请输入密码" class="int">
            <i slot="prefix" class="el-icon-lock" style="font-size:18px;
            padding-right: 5px;
            color:rgb(169, 170, 170);
            "></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="checkPass">

          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" placeholder="请再次输入密码" class="int">
            <i slot="prefix" class="el-icon-lock" style="font-size:18px;
            padding-right: 5px;
            color:rgb(169, 170, 170);
            "></i>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')" style="margin-top:20px; margin-left:50px;">确认修改</el-button>
          <el-button @click="goLogin">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
  
<script>
import axios from 'axios'
import { UpdateYanZheng, isPhoneNumber, ForgetPassword } from '../api/api'
export default {
  data() {
    var num = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手机号不能为空'))
      } else {
        const reg = /^1[3456789]\d{9}$/
        const isPhone = reg.test(value)
        value = Number(value)
        if (typeof value === 'number' && !isNaN(value)) {
          //判断是否为数字
          value = value.toString() //转换成字符串
          if (value.length < 0 || value.length > 12 || !isPhone) {
            //判断是否为11位手机号
            return callback(new Error('手机号码格式不正确'))
          }
        }
      }
    }
    var yanzheng = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('验证码不能为空'))
      }
    }

    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      ifDisable: false,
      selfCount: 60,
      ruleForm: {
        pass: '',
        checkPass: '',
        number: '',
        yanzhengma: ''
      },
      rules: {
        pass: [{ validator: validatePass, trigger: 'blur' }],
        checkPass: [{ validator: validatePass2, trigger: 'blur' }],
        number: [
          { validator: num, trigger: 'blur' },
          { pattern: /^1[3456789]\d{9}$/, message: '手机号码格式不正确', trigger: 'blur' }
        ],
        yanzhengma: [{ validator: yanzheng, trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitForm(formName) {
      // this.$refs[formName].validate(valid => {
      //   if (valid) {
      //   } else {
      //     console.log('error submit!!')
      //     return false
      //   }
      // })
      ForgetPassword(this.ruleForm.number, this.ruleForm.checkPass, this.ruleForm.yanzhengma)
        .then(res => {
          console.log(res.data)
          if (res.data.code != 200) {
            this.$message({
              message: res.data.message
            })
          } else {
            this.$message({
              message: res.data.message,
              type: 'success'
            })
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    goLogin() {
      this.$router.push('/Login')
    },
    // 获取验证码
    sendCode() {
      //校验手机号
      if (isPhoneNumber(this.ruleForm.number)) {
        this.$message({
          message: '验证码已发送',
          type: 'success'
        })
      } else {
        return
      }
      //验证码倒计时
      this.ifDisable = true
      let timer = setInterval(() => {
        if (this.selfCount == 0) {
          clearInterval(timer)
          this.ifDisable = false
          this.selfCount = 60
        }
        this.selfCount--
      }, 1000)
      //获取验证码
      UpdateYanZheng(this.ruleForm.number)
        .then(res => {
          console.log(res.data)
        })
        .catch(error => {
          console.log('验证码获取失败', error)
        })
    }
  }
}
</script>
  
<style lang="less" scoped>
/deep/.el-form-item__error {
  left: 25px;
}
.beijing {
  width: 100%;
  height: 100%;
  // height: calc(100% - 80px);
  background-image: url('../assets//image/bglogin.png');
  display: flex;
  justify-content: center;
  align-items: center;
  background-size: cover;
}

.entirety {
  width: 850px;
  height: 600px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: aliceblue;
  border-radius: 15px;

  .demo-ruleForm {
    width: 300px;
    margin-left: 50px;
  }

  .pic {
    margin-right: 10px;
  }

  h4 {
    color: #000088;
  }

  .title {
    display: flex;
    flex: space-between;
    justify-content: space-between;
    margin-bottom: 20px;
  }

  .int {
    width: 250px;
  }

  .int1 {
    width: 170px;
  }
  /deep/ .el-input__inner {
    border-top-width: 0px;
    border-left-width: 0px;
    border-right-width: 0px;
    border-bottom-width: 2px;
    background-color: transparent !important;
  }
  .yanzheng {
    width: 15%;
    // margin-left: 10px;
    padding: 5px;
  }
  .but-box {
    width: 100%;
    display: flex;
    flex-direction: row;
    justify-content: space-between;

    .textBut {
      color: #000;
      font-size: 10px;
    }
  }
  .button {
    width: 100px;
    height: 50px;
  }

  .router {
    text-decoration: none;
  }
  /deep/.timr.el-form .el-form-item_error {
    top: 21%;
    right: 25% !important;
    left: unset;
  }
}
</style>